package com.kakaxi.mybilibili.module.video.presenter;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kakaxi.mybilibili.bean.VideoBean;
import com.kakaxi.mybilibili.bean.VideoCommentBean;
import com.kakaxi.mybilibili.bean.VideoIntroBean;
import com.kakaxi.mybilibili.common.WebParamConst;
import com.kakaxi.mybilibili.module.video.model.IVideoModel;
import com.kakaxi.mybilibili.module.video.model.VideoModel;
import com.kakaxi.mybilibili.module.video.view.IVideoView;
import com.kakaxi.mybilibili.utils.LogUtil;
import com.vincestyling.netroid.Listener;
import com.vincestyling.netroid.NetroidError;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * 视频控制层实现类
 * Created by kakaxi on 16/11/20.
 */

public class VideoPresenter implements IVideoPresenter {

    /** 数据层 **/
    private IVideoModel mModel;
    /** 视图层 **/
    private IVideoView mView;

    /** 视频介绍信息类 **/
    private VideoIntroBean mVideoIntroBean;
    /** 视频播放信息类 **/
    private VideoBean mVideoBean;

    public VideoPresenter(IVideoView videoView) {
        mView = videoView;
        mModel = new VideoModel();
    }

    @Override
    public void initModel(Context context) {
        mModel.initVideo(context);
    }

    @Override
    public void initVideoData(String aid) {
        mView.showLoading();
        getVideoIntroData(aid);
    }

    @Override
    public void initVideoCommentData(String aid, int pn) {
        getVideoCommentData(aid, pn, false);
    }

    /**
     * 获取介绍数据
     *
     * @param aid 视频的a编号
     */
    private void getVideoIntroData(final String aid) {
        mModel.getVideoIntroData(aid, new Listener<JSONObject>() {

            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                String codeStr = response.getString(WebParamConst.RESPONSE_CODE);
                String errorStr = response.getString(WebParamConst.RESPONSE_ERROR);
                if (codeStr == null && errorStr == null) { // 成功
                    mVideoIntroBean = JSON.parseObject(response.toJSONString(), VideoIntroBean
                            .class);
                    mView.showVideoIntroData(mVideoIntroBean);
                    getVideoDataFromWebView(aid);
//                    getVideoDataFromBilijj(mVideoIntroBean.getCid());
                    // 获取播放数据
//                    getVideoData(mVideoIntroBean.getCid());
                } else { // 失败
                    mView.hideLoading();
                    if (errorStr != null) {
                        mView.showMessage(errorStr);
                    }
                    if (codeStr != null) {
                        mView.showMessage(response.getString(WebParamConst.RESPONSE_MESSAGE));
                    }
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
                mView.showMessage(error.getMessage());
            }
        });
    }

    /**
     * 通过webview获取视频播放地址
     *
     * @param aid 视频的aid
     */
    private void getVideoDataFromWebView(String aid) {
        mModel.getVideoDataFromWebView(aid, new Listener<String>() {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                mView.hideLoading();
                List<String> videoList = new ArrayList<>();
                videoList.add(response);
                mView.showVideoData(videoList);
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
                mView.showMessage(error.getMessage());
            }
        });
    }

    /**
     * 从bilijj获取视频网址
     *
     * @param cid 视频的c编号
     */
    private void getVideoDataFromBilijj(int cid) {
        mModel.getVideoDataFromBilijj(String.valueOf(cid), new Listener<String>() {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                mView.hideLoading();
                if (response != null) {
                    Document doc = Jsoup.parse(response);
                    if (doc != null) {
                        LogUtil.d("网址获取成功");
                        List<String> videoUrlList = new ArrayList<>();
                        // 先分析分段视频
                        Elements elements = doc.getElementsByClass("D");
                        if (elements != null && elements.size() > 0) {
                            // 获取<div class="D"
                            Element firstElement = elements.get(0);
                            // 获取<div class="D"><a><a></div>
                            Elements childElements = firstElement.children();
                            for (int i = 0; i < childElements.size(); i++) {
                                Element childElement = childElements.get(0);
                                videoUrlList.add(childElement.attr("href"));
                            }
                            mView.showVideoData(videoUrlList);
                            return;
                        }
                        // 单个视频
                        elements = doc.getElementsByClass("putong");
                        LogUtil.d("putong:" + elements);
                        if (elements != null && elements.size() > 0) {
                            Element parentElement = elements.get(0).parent();
                            videoUrlList.add(parentElement.attr("href"));
                            mView.showVideoData(videoUrlList);
                        }
                    }
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
            }
        });
    }

    /**
     * 获取视频播放数据
     *
     * @param cid 视频的c编号
     */
    private void getVideoData(int cid) {
        mModel.getVideoData(cid, new Listener<JSONObject>() {

            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                mView.hideLoading();

                String result = response.getString(WebParamConst.RESPONSE_RESULT);
                if (result != null && result.equals("suee")) {
                    mVideoBean = JSON.parseObject(response.toJSONString(), VideoBean.class);
                    mView.showVideoData(mVideoIntroBean, mVideoBean);
                } else {
                    mView.showVideoIntroData(mVideoIntroBean);
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
                mView.showVideoIntroData(mVideoIntroBean);
            }
        });
    }


    /**
     * 获取评论数据
     *
     * @param aid     a编号
     * @param pn      页数
     * @param moreHot 是否更多热门
     */
    public void getVideoCommentData(String aid, int pn, boolean moreHot) {
        mModel.getVideoCommentData(aid, pn, moreHot, new Listener<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                if (response.getString(WebParamConst.RESPONSE_CODE).equals(WebParamConst
                        .RESULT_CODE_OK)) {
                    VideoCommentBean videoCommentBean = JSON.parseObject(response
                            .getString(WebParamConst.RESPONSE_DATA), VideoCommentBean.class);
                    mView.showVideoCommentData(videoCommentBean);
                } else {
//                    mView.showMessage(response.getString(WebParamConst.RESPONSE_MESSAGE));
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
            }
        });
    }


}
